package com.dailyblue.java.project.bookstore.book.controller;

import com.dailyblue.java.project.bookstore.book.service.BookService;
import com.dailyblue.java.project.bookstore.book.util.JsonResult;
import com.dailyblue.java.project.bookstore.book.util.ResultCode;
import com.dailyblue.java.project.bookstore.book.util.ResultTool;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/book")
public class BookController {
    @Resource
    private BookService bookService;

    @GetMapping
    public JsonResult find() {
        return ResultTool.success(bookService.list());
    }

    @GetMapping("/newarrival")
    public JsonResult findBookByNewArrival() {
        return bookService.findBookByNewArrival();
    }

    @GetMapping("/id")
    public JsonResult findBookById(Integer id) {
        if (id == null) {
            return ResultTool.fail(ResultCode.COMMON_FAIL);
        }
        return bookService.findBookById(id);
    }

    @GetMapping("/id/{id}")
    public JsonResult findBookById2(@PathVariable("id") Integer id) {
        if (id == null) {
            return ResultTool.fail(ResultCode.COMMON_FAIL);
        }
        return bookService.findBookById(id);
    }
}
